Imports System.Data.SqlClient

Public Class frmCreaPeriodos

    Private Sub dgHistorial_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgHistorial.CellContentClick

    End Sub

    Private Sub dgHistorial_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgHistorial.CellEndEdit
        If dgHistorial.CurrentCell.ColumnIndex = 1 Then
            Dim Fecha As Date = Now()
            dgHistorial.CurrentRow.Cells(2).Value = Fecha
            dgHistorial.CurrentRow.Cells(3).Value = Fecha.AddDays(6)
        ElseIf dgHistorial.CurrentCell.ColumnIndex = 2 Then
            Dim Fecha As Date = CDate(dgHistorial.CurrentRow.Cells(2).Value)
            dgHistorial.CurrentRow.Cells(3).Value = Fecha.AddDays(6)
        End If
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        CargaPeriodos()

    End Sub
    Sub CargaPeriodos()
        dgHistorial.Rows.Clear()

        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = CitraConnection

        Dim strSql As String = ""
        strSql = "SELECT * FROM PERIODOS ORDER BY PERIODO"
        Application.DoEvents()
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn
        cmdBuscar.CommandText = strSql

        Dim rdBuscar As SqlDataReader
        cnconn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            dgHistorial.Rows.Add(rdBuscar("IDPERIODO"), rdBuscar("PERIODO"), rdBuscar("FECHAINICIAL"), rdBuscar("FECHAFINAL"))
        Loop
    End Sub

    Private Sub btnTodosRegresar_Click(sender As System.Object, e As System.EventArgs) Handles btnTodosRegresar.Click
        Close()

    End Sub

    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
        Dim PERIODOS As New clsPERIODOS

        Try
            For I = 0 To dgHistorial.Rows.Count - 1
                If dgHistorial.Rows(I).Cells(0).Value = 0 Then
                    PERIODOS.Insertar(dgHistorial.Rows(I).Cells(1).Value, CDate(dgHistorial.Rows(I).Cells(2).Value), CDate(dgHistorial.Rows(I).Cells(3).Value))
                End If
            Next
        Catch ex As Exception

        End Try

        Close()

    End Sub

    Private Sub frmCreaPeriodos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If PERMISOS_USUARIO(PERMISOS_PERIODOS.CREAR) = 1 Then
            dgHistorial.Enabled = True
            btnGuardar.Enabled = True
        Else
            dgHistorial.Enabled = False
            btnGuardar.Enabled = False
        End If

        CargaPeriodos()

    End Sub
End Class